package com.malex.mxtracker.component;

import com.malex.mxtracker.MxTracker;
import com.malex.mxtracker.bean.Currency;
import com.malex.mxtracker.util.Utils;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import java.util.ArrayList;

/**
 * @author Manusovich Alexander
 */
public class CurrenciesPanel extends Panel {
    public CurrenciesPanel(String id) {
        super(id);
        createCurrenciesGrid();
    }

    public void createCurrenciesGrid() {
        ArrayList<Currency> currencyArrayList = new ArrayList<Currency>();
        for (Currency c : MxTracker.getCurrencies().values()) {
            String code = c.getCharCode();
            if ("usd".equals(code)
                    || "blr".equals(code)
                    || "eur".equals(code)
                    || "uah".equals(code)
                    || "rub".equals(code)) {
                currencyArrayList.add(c);
            }
        }

        final DataView<Currency> dataView = new DataView<Currency>("currencyList",
                new ListDataProvider<Currency>(currencyArrayList)) {
            public void populateItem(final Item item) {
                final Currency currency = (Currency) item.getModelObject();
                String code = currency.getCharCode();
                if (currency.getDef() != null && currency.getDef()) {
                    code = "<b>" + code + "</b>";
                }
                item.add(new Label("currCode", code).setEscapeModelStrings(false));
                String rate = Utils.formatDoubleHTML(currency.getRate(), "00000.00");
                item.add(new Label("currRate", "" + rate).setEscapeModelStrings(false));
            }
        };
        add(dataView);
    }

}
